<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\auth;

use app\common\repositories\property\auth\propertyAuthAccessRepository;
use app\common\repositories\property\auth\propertyAuthRoleRepository;
use app\controller\property\Base;
use app\validate\property\auth\AuthAccessValidate;
use think\App;

class propertyAuthAccess extends Base
{
    protected $repository;

    public function __construct(App $app, propertyAuthAccessRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }


    /**
     * 角色列表
     * @return string|\think\response\Json
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-16 22:20
     */
    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'property_uid' => '',
                'keywords' => '',
            ]);
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        } else {
            return $this->fetch('auth/role/list', [
                'addAuth' => property_auth('propertyAuthRoleAdd'),
                'editAuth' => property_auth('propertyAuthRoleEdit'),
                'delAuth' => property_auth('propertyAuthRoleDel'),
                'detailAuth' => property_auth('propertyAuthRoleDetail')
            ]);
        }
    }

    /**
     * 添加角色
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-01-16 22:21
     */
    public function add(propertyAuthRoleRepository $roleRepository)
    {
        $propertyUId = (int)$this->request->param('property_uid');
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'property_uid' => '',
                'role_id' => '',
                'remark' => '',
            ]);
            validate(AuthAccessValidate::class)->scene('add')->check($param);
            try {
                $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加用户角色失败', $e);
                return $this->error('网络错误');
            }
        } else {
            $roleIds = $this->repository->getSearch([])
                ->where('property_id', $this->request->propertyId)
                ->where('property_uid', $propertyUId)
                ->column('role_id');
            $roleData = $roleRepository->getSearch([])
                ->where('property_id', $this->request->propertyId)
                ->where('id', 'not in', $roleIds)
                ->select()->toArray();
            return $this->fetch('property/users/role/add', [
                'roleData' => $roleData,
            ]);
        }
    }

    /**
     * 编辑角色
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-01-16 22:22
     */
    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'remark' => '',
            ]);
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑用户角色失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('property/users/role/edit', [
                'info' => $info,
            ]);

        }
    }

    /**
     * 删除角色
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-01-16 22:22
     */
    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除用户角色失败', $e);
            return $this->error('网络错误');
        }
    }

}